package com.fpx.open.platform.common.util;

import java.io.File;
import java.io.InputStream;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

public class JaxbUtil {
	private static final Map<Class<?>, JAXBContext> JAXB_CONTEXT_CACHE = new ConcurrentHashMap<Class<?>, JAXBContext>();

	public static String marshal(Object object, Class<?> clazz) {
		StringWriter out = new StringWriter();
		try {
			JAXBContext jaxbContext = getJAXBContext(clazz);
			Marshaller marshaller = jaxbContext.createMarshaller();
			marshaller.marshal(object, out);
		} catch (JAXBException e) {
			throw new RuntimeException(e);
		}
		return out.toString();
	}
	
	public static String marshal(Object object) {
		StringWriter out = new StringWriter();
		try {
			JAXBContext jaxbContext = getJAXBContext(object.getClass());
			Marshaller marshaller = jaxbContext.createMarshaller();
			marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
			marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.FALSE);
			marshaller.marshal(object, out);
		} catch (JAXBException e) {
			throw new RuntimeException(e);
		}
		return out.toString();
	}

	public void marshal(Object object, Class<?> clazz, File xmlFile) {
		if (object == null) {
			return;
		}
		if (xmlFile == null) {
			return;
		}
		try {
			JAXBContext jaxbContext = getJAXBContext(clazz);
			Marshaller marshaller = jaxbContext.createMarshaller();
			marshaller.marshal(object, xmlFile);
		} catch (JAXBException e) {
			throw new RuntimeException(e);
		}
	}

	private static JAXBContext getJAXBContext(Class<?> clazz) {
		JAXBContext jaxbContext = JAXB_CONTEXT_CACHE.get(clazz);
		if (jaxbContext == null) {
			try {
				jaxbContext = JAXBContext.newInstance(clazz);
				JAXB_CONTEXT_CACHE.put(clazz, jaxbContext);
			} catch (JAXBException e) {
				throw new RuntimeException(e);
			}
		}
		return jaxbContext;
	}

	@SuppressWarnings("unchecked")
	public static <T> T unmarshal(String xml, Class<T> clazz) {
		Object object = null;
		try {
			JAXBContext jaxbContext = getJAXBContext(clazz);
			Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
			object = unmarshaller.unmarshal(new StringReader(xml));
		} catch (JAXBException e) {
			throw new RuntimeException(e);
		}
		return (T) object;
	}

	@SuppressWarnings("unchecked")
	public static <T> T unmarshal(File xmlFile, Class<T> clazz) {
		if (xmlFile == null) {
			return null;
		}
		Object object = null;
		try {
			JAXBContext jaxbContext = getJAXBContext(clazz);
			Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
			object = unmarshaller.unmarshal(xmlFile);
		} catch (JAXBException e) {
			throw new RuntimeException(e);
		}
		return (T) object;
	}

	@SuppressWarnings("unchecked")
	public static <T> T unmarshal(InputStream xmlFile, Class<T> clazz) {
		if (xmlFile == null) {
			return null;
		}
		Object object = null;
		try {
			JAXBContext jaxbContext = getJAXBContext(clazz);
			Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
			object = unmarshaller.unmarshal(xmlFile);
		} catch (JAXBException e) {
			throw new RuntimeException(e);
		}
		return (T) object;
	}
}